package com.gxt.maket.service.impl;

import com.gxt.basic.utils.LoginContext;
import com.gxt.maket.domain.Business;
import com.gxt.maket.domain.BusinessProduct;
import com.gxt.maket.domain.Clue;
import com.gxt.maket.domain.ClueRemark;
import com.gxt.maket.dto.ClueActivityDTO;
import com.gxt.maket.dto.ClueBusinessDTO;
import com.gxt.maket.mapper.*;
import com.gxt.maket.service.IClueService;
import com.gxt.basic.service.impl.BaseServiceImpl;
import com.gxt.maket.vo.ClueSourceStatisticalVO;
import com.gxt.org.domain.Employee;
import com.gxt.prod.domain.Product;
import com.gxt.prod.mapper.ProductMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gxt
 * @since 2022-10-24
 */
@Service
public class ClueServiceImpl extends BaseServiceImpl<Clue> implements IClueService {

    @Autowired
    private ClueActivityMapper clueActivityMapper;
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ClueRemarkMapper clueRemarkMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessProductMapper businessProductMapper;


    @Override
    @Transactional
    public void clueBindActivity(ClueActivityDTO clueActivityDTO) {
        Long clueId = clueActivityDTO.getClueId();
        clueActivityMapper.deleteById(clueId);
        List<Long> activityIds = clueActivityDTO.getActivityIds();
        clueActivityMapper.batchActivity(clueId,activityIds);
    }

    @Override
    @Transactional
    public void assignClue(Clue clue) {
        clue.setState(1);
        clue.setEditTime(new Date());
        clue.setEditBy(LoginContext.getLoginUser().getId()+"");
        clueMapper.update(clue);
        ClueRemark remark = new ClueRemark();
        remark.setClueId(clue.getId());
        remark.setNoteContent(clue.getFullName()+"被分配给"+clue.getSeller().getUsername()+",操作人:"+LoginContext.getLoginUser().getUsername());
        remark.setCreateTime(new Date());
        remark.setCreateBy(LoginContext.getLoginUser().getId());
        clueRemarkMapper.insert(remark);
    }

    @Transactional
    @Override
    public void clueChangeBusiness(ClueBusinessDTO clueBusinessDTO) {
        Clue clue = clueBusinessDTO.getClueForm();
        Long productId = clueBusinessDTO.getProduct().getId();
        Product product = productMapper.loadById(productId);
        Employee loginUser = LoginContext.getLoginUser();
        // 修改线索的状态 = 2已转化商机 修改人  修改时间
        clue.setState(2);
        clue.setEditBy(loginUser.getId()+"");
        clue.setEditTime(new Date());
        clueMapper.update(clue);

        // 记录线索操作备注到线索备注表
        ClueRemark clueRemark = new ClueRemark();
        clueRemark.setNoteContent(clue.getFullName()+"已被转化为商机,操作人:"+loginUser.getUsername());
        clueRemark.setClueId(clue.getId());
        clueRemark.setCreateBy(loginUser.getId());
        clueRemark.setCreateTime(new Date());
        clueRemarkMapper.insert(clueRemark);

        // 创建商机 - 新增一条数据到t_mkt_business
        Business business = new Business();
        business.setName("客户"+clue.getFullName()+"对"+product.getName()+"产品有购买意向");
        business.setClueId(clue.getId());
        business.setProductId(product.getId());
        business.setProductName(product.getName());
        business.setProductPrice(product.getPrice());
        business.setState(0);
        businessMapper.insert(business);

        // 将产生商机这一刻的产品信息存放到 t_mkt_business_product
        BusinessProduct businessProduct = new BusinessProduct();
        BeanUtils.copyProperties(product, businessProduct);
        businessProduct.setId(null);
        businessProduct.setBusinessId(business.getId());
        businessProduct.setProductId(product.getId());
        businessProductMapper.insert(businessProduct);
    }

    @Override
    public List<ClueSourceStatisticalVO> StatisticalSource() {
        return clueMapper.StatisticalSource();
    }
}
